package com.huixue.zdb.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebmvcConfig implements WebMvcConfigurer {

  @Bean
  public CorsFilter corsFilter() {
      final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
      final CorsConfiguration corsConfiguration = new CorsConfiguration();
      corsConfiguration.setAllowCredentials(true);
      /*允许访问的客户端域名*/
      corsConfiguration.addAllowedOrigin("*");
      corsConfiguration.addAllowedOrigin("http://192.168.9.106:9528/");
      /*允许服务端访问的客户端请求头*/
      corsConfiguration.addAllowedHeader("*");
      corsConfiguration.addAllowedMethod("OPTIONS");
      corsConfiguration.addAllowedMethod("HEAD");
      corsConfiguration.addAllowedMethod("GET");
      corsConfiguration.addAllowedMethod("PUT");
      corsConfiguration.addAllowedMethod("POST");
      corsConfiguration.addAllowedMethod("DELETE");
      corsConfiguration.addAllowedMethod("PATCH");
      /*允许访问的方法名,GET POST等*/
      corsConfiguration.addAllowedMethod("*");
      urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
      return new CorsFilter(urlBasedCorsConfigurationSource);
  }

}
